//
//  JXVisitorView.swift
//  weibo
//
//  Created by admin on 16/5/20.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

import SnapKit

@objc protocol JXVisitorViewDelegate:NSObjectProtocol{

    
    optional func willLogin()
    
    optional func willRegister()


}

class JXVisitorView: UIView {
    
    
    //声明代理属性
    //代理对象 必须使用弱引用  在swift属性的默认就是强引用
    weak var visitorDelegate: JXVisitorViewDelegate?
    
	override init(frame: CGRect) {
		super.init(frame: frame)
        
        let color:CGFloat = 237/255.0
        
        self.backgroundColor = UIColor(red: color, green: color, blue: color, alpha: 1)
		setupUI()

	}

	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
    
    //外部可访问的方法,用来区分访客页面
    func setupVisitorView(isHome:Bool, title:String, imageName:String){
        
        if isHome {
            
            openAnimation()

            
        }else{
        
            circleView.hidden = true
        
        
        }
        
        
        textlabel.text = title

        homeView.image = UIImage(named: imageName)
        
        
    }
    
	// 添加约束控件
	private func setupUI() {

		addSubview(circleView)
        
        addSubview(hideView)

		addSubview(homeView)

		addSubview(textlabel)
        
        addSubview(loginBtn)
        
        addSubview(registerBtn)
        

		// 约束
		circleView.snp_makeConstraints { (make) in

			make.centerX.equalTo(self.snp_centerX)
			make.centerY.equalTo(self.snp_centerY).offset(-80)
		}
        
        hideView.snp_makeConstraints { (make) in
            make.center.equalTo(circleView.snp_center)
        }

		homeView.snp_makeConstraints { (make) in
			make.center.equalTo(circleView.snp_center)
		}
        
        
        textlabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(homeView.snp_centerX)
            make.centerY.equalTo(homeView.snp_centerY).offset(80)
//            make.width.equalTo(240)
            make.height.equalTo(100)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(textlabel.snp_left)
            make.top.equalTo(textlabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        loginBtn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(textlabel.snp_right)
            make.top.equalTo(textlabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.addTarget(self, action: #selector(registerBtnClick), forControlEvents: .TouchUpInside)
        

	}
    
    //点击方法
    
    @objc private func loginBtnClick(){
    
        //相当于oc中的判断是否有代理实现方法
        visitorDelegate?.willLogin?()
        
    
    }
    
    @objc private func registerBtnClick(){
    
    
        visitorDelegate?.willRegister?()
    
    }
    
    
    
    //动画
    
    private func openAnimation(){
    
        let baseAN = CABasicAnimation(keyPath: "transform.rotation")
    
        baseAN.duration = 15.0
        
        baseAN.repeatCount = MAXFLOAT
        
        baseAN.toValue = M_PI*2
        
        //当动画结束 或者 视图处于非活跃状态 动画效果不移除图层
        baseAN.removedOnCompletion = false

        circleView.layer.addAnimation(baseAN, forKey: nil)
        

    
    }

	// 懒加载控件

	lazy var circleView: UIImageView = {

		let circleView = UIImageView()

		circleView.image = UIImage(named: "visitordiscover_feed_image_smallicon")

		return circleView

	}()

	// 遮盖图片
	lazy var hideView: UIImageView = {

		let hideView = UIImageView()

		hideView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")

		return hideView

	}()

	lazy var homeView: UIImageView = {

		let homeView = UIImageView()

		homeView.image = UIImage(named: "visitordiscover_feed_image_house")

		return homeView
	}()

	lazy var loginBtn: UIButton = {

		let loginBtn = UIButton()

		loginBtn.setTitle("登录", forState: .Normal)

		loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //代码拉伸图片
        let image = UIImage(named: "common_button_white_disable")!
        
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        loginBtn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
		return loginBtn

	}()

	lazy var registerBtn: UIButton = {

		let registerBtn = UIButton()

		registerBtn.setTitle("注册", forState: .Normal)

		registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        
        //代码拉伸图片
        let image = UIImage(named: "common_button_white_disable")!
        
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        registerBtn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)

		return registerBtn

	}()

	lazy var textlabel: UILabel = {

		let text = UILabel()
        
        text.numberOfLines = 0
        
        text.textAlignment = .Center
        
        text.textColor = UIColor.darkGrayColor()
        
        text.preferredMaxLayoutWidth = 230

		text.font = UIFont.systemFontOfSize(15.0)
        
        text.sizeToFit()

		return text

	}()

}
